package com.freemalll.merchant.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freemalll.merchant.common.CommonEnum;
import com.freemalll.merchant.domain.dto.MerchantRateDetailDto;
import com.freemalll.merchant.domain.dto.MerchantRateDto;
import com.freemalll.merchant.domain.dto.coopManagement.AssetRateDetailEditDto;
import com.freemalll.merchant.domain.entity.MerMerchantRateManageDetail;
import com.freemalll.merchant.mapper.MerMerchantRateManageDetailMapper;
import com.freemalll.merchant.service.MerMerchantRateManageDetailService;
import com.freemalll.merchant.utils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 商户费率新表明细 服务实现类
 * </p>
 *
 * @author songshixiong
 * @since 2025-07-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MerMerchantRateManageDetailServiceImpl extends ServiceImpl<MerMerchantRateManageDetailMapper, MerMerchantRateManageDetail> implements MerMerchantRateManageDetailService {


    @Override
    public void batchInsert(List<AssetRateDetailEditDto> detailEditDtoList, String merRateId) {
        if (CollectionUtils.isEmpty(detailEditDtoList)) {
            return;
        }
        detailEditDtoList.forEach(item -> {
            MerMerchantRateManageDetail entity = new MerMerchantRateManageDetail();
            entity.setMerRateId(merRateId);
            entity.setTimes(item.getTimes());
            entity.setInterestRate(new BigDecimal(item.getInterestRate()));
            entity.setEnableFlag(true);
            this.save(entity);
        });
    }

    @Override
    public List<MerchantRateDetailDto> queryListInMerRateId(List<String> rateIds, String type) {
        if (CollectionUtils.isEmpty(rateIds)) {
            return Collections.emptyList();
        }
        if (CommonEnum.rateModel.IRR.name().equals(type)) {
            return baseMapper.queryListInMerRateIdByIRR(rateIds);
        } else {
            return baseMapper.queryListInMerRateIdByAPR(rateIds);
        }
    }

    @Override
    public void batchSaveOrUpdate(MerchantRateDto dto, String merRateId) {
        List<MerchantRateDetailDto> details = new ArrayList<>();
        if (dto.getPhaseThere() != null) {
            details.add(dto.getPhaseThere());
        }
        if (dto.getPhaseSix() != null) {
            details.add(dto.getPhaseSix());
        }
        if (dto.getPhaseNine() != null) {
            details.add(dto.getPhaseNine());
        }
        if (dto.getPhaseTwelve() != null) {
            details.add(dto.getPhaseTwelve());
        }
        if (dto.getPhaseEighteen() != null) {
            details.add(dto.getPhaseEighteen());
        }
        if (dto.getPhaseTwentyFour() != null) {
            details.add(dto.getPhaseTwentyFour());
        }
        if (dto.getPhaseThirtySix() != null) {
            details.add(dto.getPhaseThirtySix());
        }
        if (CollectionUtils.isEmpty(details)) {
            return;
        }
        details.forEach(item -> {
            if (StringUtils.hasText(item.getRateDetailId())) {
                MerMerchantRateManageDetail merMerchantRateManageDetail = baseMapper.selectById(item.getRateDetailId());
                BeanUtils.copyPropertiesIgnoreNull(item, merMerchantRateManageDetail);
                merMerchantRateManageDetail.setMerRateId(merRateId);
                merMerchantRateManageDetail.setEnableFlag(true);
                baseMapper.updateById(merMerchantRateManageDetail);
            } else {
                MerMerchantRateManageDetail merMerchantRateManageDetail = new MerMerchantRateManageDetail();
                BeanUtils.copyPropertiesIgnoreNull(item, merMerchantRateManageDetail);
                merMerchantRateManageDetail.setMerRateId(merRateId);
                merMerchantRateManageDetail.setEnableFlag(true);
                baseMapper.insert(merMerchantRateManageDetail);
            }
        });
    }
}
